package org.example.onlinechat.service;

import com.volcengine.ark.runtime.service.ArkService;
import org.example.onlinechat.entity.OrdMsg;
import org.example.onlinechat.mapper.ChatMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChatService {

    @Autowired
    ChatMapper  chatMapper;

    public ArkService ChatSer(){
        String apiKey = System.getenv("ARK_API_KEY");
        return ArkService.builder().apiKey(apiKey).build();
    }

    public  void  insertChatMessage(String user_id, String json){
        if (chatMapper.selectData(Integer.parseInt(user_id))==null)
        {
            chatMapper.insert(user_id,json);
        }else {
            chatMapper.UpdataChat(user_id,json);
        }
    }


    public String selectData(int user_id) {
        return chatMapper.selectData(user_id);
    }

    public void ChatCreate(int userId) {
        String data =chatMapper.selectData(userId);
        chatMapper.delete(userId);
        if (data==null){
            return;
        }
        chatMapper.insertOChat(userId,data);
    }

    public List<OrdMsg> selectOrdData(int user_id) {
        return chatMapper.selectOrdData(user_id);
    }

    public String chatSelectOrdN(OrdMsg msg) {
        return  chatMapper.chatSelectOrdN(msg);
    }

    public List<OrdMsg> SALL(){
        return chatMapper.selectAllOrdData();
    }

}
